var groupicon = $webroot + "resources/img/communication.gif";


(function($){
	/**
	 * 获取form数据
	 */
	$.formParams = function(formId){
		if (formId==null || formId == undefined || typeof(formId) != 'string') {
			return;
		}
		var form = liger.get(formId);
		if(form==null || typeof(form) == 'undefined'){
			return ;
		}
		return form.getData();
	};
	/**设置form数据
	 * 
	 */
	$.formDatas = function(opts){
		var formId = opts.formId;
		var datas = opts.datas;
		if (formId==null || formId == undefined || typeof(formId) != 'string') {
			return;
		}
		var form = liger.get(formId);
		if(form==null || typeof(form) == 'undefined'){
			return ;
		}
		 form.setData(datas);
	};
	
	/**
	 * 重置表单form
	 */
	$.resetFrom = function(opts){
		var formId = opts.formId;
		var datas = opts.datas || {};
		if (formId==null || formId == undefined || typeof(formId) != 'string') {
			return;
		}
		var form = liger.get(formId);
		if(form==null || typeof(form) == 'undefined'){
			return ;
		}
		form.setData(datas);
	
	};
	
	/**
	 * 弹出框
	 */
	$.openDialog = function(opts){
		opts = $.extend({
			title:'',
			width:$(window).width()-450,
			height:$(window).height()-150,
			showMax:true,
			showMin:true,
			showToggle:true,
			isResize:true,
			slide: false,
			buttons:null,
			data:null
		},opts);
		$.ligerDialog.open({
			showMax: opts.showMax,
			showMin: opts.showMin,
			showToggle: opts.showToggle,
            isResize: opts.isResize,
            slide: opts.slide,
			width:opts.width,
	 		height:opts.height,
	 		title:opts.title,
	 		url:opts.url,
	 		data:opts.data,
	 		buttons:opts.buttons
		});
	};
	/**
	 * 新增tab页
	 */
	$.addTab = function(opts){
		opts = $.extend({
			tabid:Date.parse(new Date()),
			text:Math.random(),
			url:"#"
		},opts);
		top.f_addTab(opts.tabid,opts.text,opts.url);
	};
	/**
	 * 关闭当前tab页
	 */
	$.closeCurrentTab = function(){
		top.closeCurrentTab(null);
	};
	
	/**
	 * 树
	 */
	$.fn.extend({
		esTree : function(opts){
			opts = $.extend({
				checkbox: false,
		        slide: false,
		        idFieldName: 'id',
		        parentIDFieldName: 'parentId',
		        textFieldName:'name',
		        isExpand:function(data,level){if(data.data.expand==1){return true}else{return false}},
		        nodeWidth: 180
			},opts);
			return $(this).ligerTree({
				url: opts.url,
				data:opts.data,
				parms:opts.parms,
		        checkbox: opts.checkbox,
		        attribute:opts.attribute,
		        slide: opts.slide,
		        idFieldName: opts.idFieldName,
		        parentIDFieldName: opts.parentIDFieldName,
		        textFieldName:opts.textFieldName,
		        nodeWidth: opts.nodeWidth,
		        isExpand:opts.isExpand,
		        onBeforeExpand:opts.onBeforeExpand,
		        onSelect:opts.onSelect,
		        sortName:opts.sortName,							//排序字段
				sortOrder:opts.sortOrder
			});
		}
	});
	
	/**
	 * 树 表格
	 */
	$.fn.extend({
		esTreeGrid : function(opts){
			opts = $.extend({
				root: 'rows',                       //数据源字段名
        		record: 'total',                     //数据源记录数字段名
				height:'98%',
				dataAction : 'server',
				rownumbers:true,
				colDraggable : true,
				checkbox : true,
				selectRowButtonOnly:true,
				alternatingRow: false,
				usePager:false,
				pageSize : 15,
				pageSizeOptions : [5,10,15,20,25,30,50,100],
				pageParmName: "page",
				pagesizeParmName : "pageSize",
				sortnameParmName : "sortName",
				sortorderParmName: "sortOrder",
				tree: {
                    columnId: 'name',
                    idField: 'id',
                    parentIDField: 'parentId'
                }
                
			},opts);
			$.each(opts.columns,function(n,column){
				if(!column.minWidth){
					column.minWidth=60;
				}
				if(!column.align){
					column.align = "left";
				}
			});
			if(opts.toolbar){
				var items = opts.toolbar.items;
				$.esAjax({
					async:false,
					url:$webroot + 'sysPermission/querySysPermissionBtns.do',
					data:{gridId:opts.id},
					success:function(data){
						items = $.grep(items,function(item,i){
							if(item.id){
								if($.inArray(item.id, data)!= -1 ){
									return item;
								};
							};
						});
					}
				});
				opts.toolbar.items = items;
			};
			return $(this).ligerGrid({
				root: opts.root,                       //数据源字段名
        		record: opts.record,                     //数据源记录数字段名
				pageParmName: opts.pageParmName,               //页索引参数名，(提交给服务器)
	        	pagesizeParmName: opts.pagesizeParmName,        //页记录数参数名，(提交给服务器)
	        	sortnameParmName: opts.sortnameParmName,        //页排序列名(提交给服务器)
	        	sortorderParmName: opts.sortorderParmName,      //页排序方向(提交给服务器) 
				height:opts.height,
				columns:opts.columns,
				dataAction:opts.dataAction,
				url:opts.url,
				usePager:opts.usePager,
				pageSize:opts.pageSize,
				pageSizeOptions:opts.pageSizeOptions,
				rownumbers:opts.rownumbers,
				colDraggable:opts.colDraggable,
				checkbox:opts.checkbox,
				onAfterShowData:opts.onAfterShowData,
				selectRowButtonOnly:opts.selectRowButtonOnly,
				alternatingRow:opts.alternatingRow,
				sortName:opts.sortName,							//排序字段
				sortOrder:opts.sortOrder,
				toolbar:opts.toolbar,
				tree : opts.tree
			});
		}
	});
	
	
	/**
	 * grid
	 */
	$.fn.extend({
		esSimpleGrid:function(opts){
			opts = $.extend({
				root: 'rows',                       //数据源字段名
        		record: 'total',                     //数据源记录数字段名
				height:'98%',
				dataAction : 'server',
				rownumbers:true,
				colDraggable : true,
				checkbox : true,
				selectRowButtonOnly:true,
				enabledEdit:false,
				switchPageSizeApplyComboBox:true,
				pageSize : 15,
				pageSizeOptions : [10,15,20,25,30,50,100],
				pageParmName: "page",
				pagesizeParmName : "pageSize",
				sortnameParmName : "sortName",
				sortorderParmName: "sortOrder"
			},opts);
			$.each(opts.columns,function(n,column){
				if(!column.minWidth){
					column.minWidth=60;
				}
				
			});
			if(opts.toolbar){
				var items = opts.toolbar.items;
				$.esAjax({
					async:false,
					url:$webroot + 'sysPermission/querySysPermissionBtns.do',
					data:{gridId:opts.id},
					success:function(data){
						items = $.grep(items,function(item,i){
							if(item.id){
								if($.inArray(item.id, data)!= -1 ){
									return item;
								};
							};
						});
					}
				});
				opts.toolbar.items = items;
			};
			return $(this).ligerGrid({
				root: opts.root,                       //数据源字段名
        		record: opts.record,                     //数据源记录数字段名
				title:opts.title,
				pageParmName: opts.pageParmName,               //页索引参数名，(提交给服务器)
	        	pagesizeParmName: opts.pagesizeParmName,        //页记录数参数名，(提交给服务器)
	        	sortnameParmName: opts.sortnameParmName,        //页排序列名(提交给服务器)
	        	sortorderParmName: opts.sortorderParmName,      //页排序方向(提交给服务器) 
	        	width:opts.width,
				height:opts.height,
				columns:opts.columns,
				dataAction:opts.dataAction,
				switchPageSizeApplyComboBox:opts.switchPageSizeApplyComboBox, //切换每页记录数是否应用ligerComboBox
				isScroll:opts.isScroll, //设置为false时将不会显示滚动条，高度自适应
				delayLoad:opts.delayLoad, //初始化是是否不加载
				url:opts.url,
				usePager:opts.usePager,
				pageSize:opts.pageSize,
				pageSizeOptions:opts.pageSizeOptions,
				rownumbers:opts.rownumbers,
				colDraggable:opts.colDraggable,
				onAfterShowData:opts.onAfterShowData,
				checkbox:opts.checkbox,
				enabledEdit: opts.enabledEdit,				//编辑
				onBeforeEdit:opts.onBeforeEdit,				//编辑前事件
				onBeforeSubmitEdit:opts.onBeforeSubmitEdit, //编辑提交前事件
				onAfterEdit: opts.onAfterEdit,				//编辑后事件
				selectRowButtonOnly:opts.selectRowButtonOnly,
				onSelectRow:opts.onSelectRow,					//选中事件
				onDblClickRow:opts.onDblClickRow,				//双击事件
				parms:opts.parms,								//参数
				sortName:opts.sortName,							//排序字段
				sortOrder:opts.sortOrder,
				toolbar:opts.toolbar
			});
		}
	});
	
	/**
	 * 表单form
	 */
	$.fn.extend({
		esForm:function(opts){
			opts = $.extend({
				 inputWidth: 160, 
				 labelWidth: 80, 
				 space: 40,
				 validate : true,
				 doSave:null,
				 doCancel:null
			},opts);
			$.each(opts.fields,function(n,field){
				if(!field.newline){
					field.newline=false;
				}
				if(field.type=="select"){
					field.comboboxName=field.name
				}
				field.id = field.name;
			});
			if(!opts.buttons){
				opts.buttons = [];
				opts.buttons.push({text: '确定', click: opts.doSave});
				opts.buttons.push({text: '取消', click: opts.doCancel});
			}
			return $(this).ligerForm({
				inputWidth:opts.inputWidth,
				labelWidth:opts.labelWidth,
				validate:opts.validate,
				readonly:opts.readonly,
				space:opts.space,
				fields:opts.fields,
				buttons:opts.buttons
			});
		}
	});
	
	/**
	 * 查询form
	 */
	$.fn.extend({
		esQueryForm:function(opts){
			opts = $.extend({
				inputWidth: 120, 
				labelWidth: 60, 
				space: 40,
				validate : true,
				doSearch:null,
				doReset:null
			},opts);
			$.each(opts.fields,function(n,field){
				if(0==n){
					field.group="查询条件";
					field.groupicon=groupicon;
					field.newline=true;
				}else{
					field.newline=false;
				}
				field.id = field.name;
			});
			if(!opts.buttons){
				opts.buttons = [];
				opts.buttons.push({text: '查询',  click: opts.doSearch});
				opts.buttons.push({text: '重置',  click: opts.doReset});
			}
			return $(this).ligerForm({
				validate:opts.validate,
				inputWidth:opts.inputWidth,
				labelWidth:opts.labelWidth,
				space:opts.space,
				fields:opts.fields,
				buttons:opts.buttons
			});
		}
	});
	
	
})(jQuery);